<template>
  <div>
    <el-dialog :title="dialogAddOrUpdateModel.title" :visible.sync="dialogAddOrUpdateModel.show" width="40%" @open="openFun"
    :close-on-click-modal="false" :close-on-press-escape="false" :append-to-body="true">
      <el-form ref="form" :model="modelData" label-width="80px" :rules="rules">
        <el-form-item label="型号名称" prop="modelName">
        <el-input v-model="modelData.modelName" placeholder="型号名称"></el-input>
        </el-form-item>
        <el-form-item label="型号编码" prop="modelCode">
        <el-input v-model="modelData.modelCode" placeholder="型号编码" v-if="modelData.modelId != null" disabled></el-input>
        <el-input v-model="modelData.modelCode" placeholder="型号编码" v-if="modelData.modelId == null"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="status">
            <el-radio-group v-model="modelData.status">
              <el-radio-button label="1">是</el-radio-button>
              <el-radio-button label="2">否</el-radio-button>
            </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" @click="handleSubmit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    dialogAddOrUpdateModel: Object,
    modelData: Object
  },
  data() {
    return {

      rules: {
          modelName: [
            { required: true, message: '型号名称不能为空', trigger: 'blur' },
            { min: 2, max: 60, message: "长度在 2 到 60 个字符", trigger: "blur" }
          ],
          modelCode: [
            { required: true, message: '型号编码不能为空', trigger: 'blur' },
            { min: 2, max: 60, message: "长度在 2 到 30 个字符", trigger: "blur" }
          ],
          status: [
            { required: true, message: '是否启用不能为空', trigger: 'blur' }
          ]
      }
    }
  },
  methods: {
    // 弹窗打开触发的方法
    openFun() {

    },

    // 点击确定按钮触发
    handleSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialogAddOrUpdateModel.option == 'add') {
            this.$emit("handleSubmit")
          } else {
            this.$emit("handleSubmitEdit")
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 点击取消按钮触发
    resetForm(formName) {
      this.dialogAddOrUpdateModel.show = false
      this.$refs[formName].resetFields();
    }
  },
}
</script>

<style lang="scss" scoped>
</style>